<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px" @close="close">
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="房间" prop="houseRoomId">
        <el-input v-model.trim="form.houseRoomId" autocomplete="off" />
      </el-form-item>
      <el-form-item label="单价" prop="pice">
        <el-input v-model.trim="form.pice" autocomplete="off" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model.trim="form.unit" autocomplete="off" />
      </el-form-item>
      <el-form-item label="计费日期" prop="date">
        <el-input v-model.trim="form.date" autocomplete="off" />
      </el-form-item>
      <el-form-item label="图片地址" prop="url">
        <el-input v-model.trim="form.url" autocomplete="off" />
      </el-form-item>
      <el-form-item label="读数" prop="count">
        <el-input v-model.trim="form.count" autocomplete="off" />
      </el-form-item>
      <el-form-item label="收费项状态" prop="type" label-align="right">
        <el-radio-group class="radioMar" v-model="form.type">
          <el-radio :label="'1'">水费</el-radio>
          <el-radio :label="'2'">电费</el-radio>
          <el-radio :label="'3'">网络费</el-radio>
          <el-radio :label="'4'">管理费</el-radio>
          <el-radio :label="'5'">其他按月收费</el-radio>
          <el-radio :label="'6'">其他固定费用</el-radio>
          <el-radio :label="'7'">其他人工计算</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="收费项状态" prop="state">
        <el-radio-group v-model="form.state">
          <el-radio :label="0">初始</el-radio>
          <el-radio :label="1">每个月新添加</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/userManagement'

  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        form: {
          houseRoomId: '',
          pice: '',
          unit: '',
          date: '',
          imageId: '',
          url: '',
          count: '',
          payName: '',
          type: '',
          state: '',
        },
        rules: {
          username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
          password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
          email: [{ required: true, trigger: 'blur', message: '请输入邮箱' }],
          permissions: [{ required: true, trigger: 'blur', message: '请选择权限' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            findPayName(this.form.type)
            const { msg } = await doEdit(this.form)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },

      findPayName(type) {
        // 可以根据实际需求进行特殊类型的格式化
        if (type === '1') {
          this.form.payName = '水费'
        } else if (type === '2') {
          this.form.payName = '电费'
        } else if (type === '3') {
          this.form.payName = '网络费'
        } else if (type === '4') {
          this.form.payName = '管理费'
        } else if (type === '5') {
          this.form.payName = '其他按月收费'
        } else if (type === '6') {
          this.form.payName = '其他固定费用'
        } else if (type === '7') {
          this.form.payName = '其他人工计算'
        }
      },
    },
  }
</script>

<style lang="scss" scoped>
  .radioMar {
    margin-top: -10px;

    .el-radio {
      margin-top: 10px;
    }
  }
</style>
